require('./index.css')
require('page/common/nav/index.js')
require('page/common/header/index.js')
var _mm = require('util/mm.js')
var _order = require('service/order-service.js')
var _address = require('service/address-service.js')
var templateAddress = require('./address-list.string')
var templateProduct = require('./address-product.string')
var addressModal = require('./address-modal.js')

var page = {
    data: {
        selectAddressId: null
    },
    init: function () {
        this.onLoad();
        this.bindEvent();
    },
    onLoad: function () {
        this.loadAddressList()
        this.loadProductList()
    },
    bindEvent: function () {
        var _this = this;
        // 地址的选择
        $(document).on('click', '.address-item', function () {
            $(this).addClass('active').siblings('.address-item').removeClass('active')
            _this.data.selectAddressId = $(this).data('id')
        })
        // 订单提交
        $(document).on('click', '.order-submit', function () {
            // 先判断地址是否存在
            var shippingId = _this.data.selectAddressId;
            if (shippingId) {
                _order.createOrder({
                    shippingId: shippingId
                }, function (res) {
                    // 成功的话直接跳转到支付页面 并携带订单号
                    window.location.href = './payment.html?orderNumber=' + res.orderNo
                }, function (errMsg) {
                    _mm.errorTips(errMsg)
                })
            } else {
                _mm.errorTips('请选择地址后再提交')
            }
        })
        // 点击添加地址
        $(document).on('click', '.address-add', function () {
            addressModal.show({
                isUpdate: false,
                onSuccess: function () {
                    // 成功的回调函数 重新加载主页的addressList
                    _this.loadAddressList();
                }
            })
        });
        // 地址的编辑
        $(document).on('click', '.address-update', function (e) {
            if(e&&e.stopPropagation){
                e.stopPropagation()
            }else if(window.event){
                window.event.cancelBubble=true;
            }
        //   e.stopPropagation()
            // 需要读取这个地址的信息 然后回填到表单中
            var shippingId = $(this).parents('.address-item').data('id')
            _address.getAddress(shippingId, function (res) {
                //显示弹出框
                addressModal.show({
                    isUpdate    : true,
                    data        : res,
                    onSuccess   : function () {
                        // 成功的回调函数 重新加载主页的addressList
                        _this.loadAddressList();
                    }
                })
            }, function (errMsg) {
                _mm.errorTips(errMsg)
            });

        });
        // 地址的删除
        $(document).on('click','.address-delete',function(e){
            if(e&&e.stopPropagation){
                e.stopPropagation()
            }else if(window.event){
                window.event.cancelBubble=true;
            }
            var id = $(this)
            .parents('.address-item').data('id')
            if(window.confirm('确认要删除该地址吗?')){
                _address.deleteAddress(id,function(res){
                    _this.loadAddressList();
                },function(errMsg){
                    _mm.errorTips(errMsg)
                })
            }
        })
    },
    // 加载收货地址
    loadAddressList: function () {
        var _this = this;
        $('.address-con').html('<div class="loading"></div>')
        _address.getAddressList(function (res) {
            //把选择地址保存的id回传过去 保证页面刷新之后选择的默认地址还在
            _this.addressFilter(res)
            var addressListHtml = _mm.renderHtml(templateAddress, res);
            $('.address-con').html(addressListHtml)
        }, function (errMsg) {
            $('.address-con').html('<p class="err-tip">地址加载失败，请刷新重试</p>')
        })
    },
    // 处理地址列表选中状态
    addressFilter : function(res){
        if(this.data.selectAddressId){
            //设置一个标记
            var selectAddressFlag = false ;
            for(var i = 0, length = res.list.length ; i<length;i++){
                //点击保存的地址的id和获取到的地址列表的id一样的话 证明这个列表被点击
                if(this.data.selectAddressId === res.list[i].id){
                    res.list[i].isActive = true ;
                    selectAddressFlag = true ;
                }
            };
            // 如果以前选中的地址不在列表中 将其删除
            if(!selectAddressFlag){
                this.data.selectAddressId = null 
            }
        }
    },
    // 加载产品list
    loadProductList: function () {
        $('.product-con').html('<div class="loading"> </div>')
        _order.getProductList(function (res) {
            var ProductListHtml = _mm.renderHtml(templateProduct, res);
            $('.product-con').html(ProductListHtml)
        }, function (errMsg) {
            $('.address-con').html('<p class="err-tip">商品列表加载失败，请刷新重试</p>')
        })
    }

}

$(function () {
    page.init()
})